<?php
/**
 * 
 * 根据话题抓取微博数据
 * @author yangchao
 * @since 2012-03-19
 */
if (!file_exists("../SINASRV_CONFIG")) {
	echo "Read the sinasrv config error!\n";
//	exit();
} else {
   $array_config = parse_ini_file("../SINASRV_CONFIG", true); var_dump($array_config);
   
   $_SERVER = array_merge($_SERVER, $array_config); 
}
echo "<pre>";

/*ini_set("display_errors",ture);
error_reporting(E_ALL);*/

define("CRON_NAME","sina_meinv_");//cro名称，要求唯一
set_time_limit(0);
$_GET["db_debug"] = true;
$_GET["no_pref_debug"] = true;
//$_GET["api_debug"] = true;

//15分钟一次,执行1000条
define("PCOUNT",10);//每次处理多少mid
define("MAX_PAGE",1000); //定义每个进程处理最大的页数
define("TOTAL_PROCESS",1);//添加新进程需要更新这个配置
define("NOW_PROCESS",$argv[1] ? $argv[1] : 0);//当前进程ID=0 可以申请如下php xxx 1
define("CRON_START_TIME",time());



include_once($_SERVER['DOCUMENT_ROOT'] ."/include/user/inc.php");
include_once($_SERVER['DOCUMENT_ROOT'] ."/include/tucao/inc.php");
include_once($_SERVER['DOCUMENT_ROOT'] ."/include/mblog/inc.php");
include_once($_SERVER['DOCUMENT_ROOT'] ."/include/qq/inc.php");
class mod_cron_engine_main extends mod_cron_engine
{
	function __construct($current_process_id)
	{
		parent::__construct($current_process_id,CRON_NAME);
	}
	
	
	function craw()
	{
		$this->get_data();
	}
	
	
	function  run()
	{
		//开始跑cron之前预先获取数据（start_id等）
		$this->before_run();
		
		//正式获取数据
		$this->craw();
		
		
		$this->after_run();
	}
	
	
	function get_data()
	{
		
		$db_mblog_meinv_topic = new db_mblog_meinv_topic();
		$mblog_from_api = new mblog_from_api();
		
		//获取tag的总数
		$count = $db_mblog_meinv_topic->cron_get_mblog_topic_count($this->start_id);
		
		if($count == 0)
		{
			//如果获取不到需要更新的数据，初始化cron_start_id
			$this->ini_start_id(NOW_PROCESS);
			
			$this->start_id = 0 ;
			$count = $db_mblog_meinv_topic->cron_get_mblog_topic_count($this->start_id);
		}
		
		
		
		echo "tag_count:$count<br>\n";
		$total_page = ceil($count/PCOUNT);
		if($total_page > MAX_PAGE && defined("MAX_PAGE")) $total_page = MAX_PAGE;
			
		for($i=1;$i<=$total_page;$i++)
		{
			$this->get_list_by_page($i);
		}
		
		
	}	
	
	function get_list_by_page($p)
	{
		$db_mblog_meinv_topic = new db_mblog_meinv_topic();
		$db_cron_get_manage = new db_cron_get_manage();
		//分批次取tag名称
		$food_tag = $db_mblog_meinv_topic->cron_get_mblog_topic_list($this->start_id,$p,PCOUNT);
		
		foreach($food_tag as $value)
		{
			$max_time = "";
			//执行时间超过60分钟，退出
			if(time() - CRON_START_TIME > 3600)
			{
				exit;
			}
			$topic = $value["topic"];
			$id = $value["id"];
			$cron_start_time  = $value["cron_start_time"];
			if($id % TOTAL_PROCESS != NOW_PROCESS)//用总进程做控制 example: 3个进程  php xxx 1 只会执行除3余1的id
			{
				continue;
			}
			$black_word  = $value["black_word"];
			$show_type = $value["show_type"];
			if($topic)//处理抓取分页逻辑
			{
				
				
				$get_key = CRON_NAME.$topic;
				$get_res = $db_cron_get_manage->cron_get_item_by_key($get_key);
				$cron_start_time = $get_res["get_num"];
				$cron_end_time = $get_res["get_num_end"];
				
				$this->get_list_by_topic($topic,$id,1,$cron_start_time,$show_type,$black_word,$get_key,1);
				$this->get_list_by_topic($topic,$id,1,$cron_end_time,$show_type,$black_word,$get_key,2);
				
				
				
			}
	
			if($id)//更新抓取的start_id到数据库
			{
				$done_job = true;
				$res = $this->replace_into_data($id);	
			}
			
			
		}
		
		if($done_job == false)
		{
			$this->ini_start_id();
		}
			
	}
	
	
	//根据话题抓取数据并入库
	function get_list_by_topic($topic,$id,$p=1,$starttime="",$show_type="",$black_word="",$get_key="",$get_type="")
	{
		global $config;
		
		var_dump("$topic,$id,$p,$starttime,$show_type,$black_word,$get_key,$get_type");
		
		
		$db_cron_get_manage = new db_cron_get_manage();
		$db_base = new db_base();
		$mblog_from_api = new mblog_from_api();
		$db_user_info = new db_user_info();
		$db_mblog_meinv_content = new db_mblog_meinv_content();
		$user_info = new user_info();
		
		$max_time = "";
		$min_time = "";
			
			
		if($topic)
		{
			
			if(!$starttime)
			{
				$starttime = strtotime($starttime["create_time"]);
				
			}
			if($get_type == 2)
			{
				//根据话题抓取
				$mblog_list = $mblog_from_api->api_get_mblog_by_topic($topic,$ids="",$sort="",$fuid="",$province="",$city="","",$starttime,
					$hasori=1,
					$hasret="",
					$hastext="",
					$haspic=1,
					$hasvideo="",
					$hasmusic="",
					$haslink="",
					$hasat="",
					$hasv="",
					$istag="",
					$dup="",
					$antispam="",
					$base_app=0,
					$p,
					$pcount=50
					);
					
			}
			else
			{
				//根据话题抓取
				$mblog_list = $mblog_from_api->api_get_mblog_by_topic($topic,$ids="",$sort="",$fuid="",$province="",$city="",$starttime,$endtime="",
					$hasori=1,
					$hasret="",
					$hastext="",
					$haspic=1,
					$hasvideo="",
					$hasmusic="",
					$haslink="",
					$hasat="",
					$hasv="",
					$istag="",
					$dup="",
					$antispam="",
					$base_app=0,
					$p,
					$pcount=50
					);
			}
		
				
			
			echo "{$topic}:count:{$mblog_list["data"]["count"]}|get_count".count($mblog_list["data"]["result"]).":p:{$p}<br>\n";	
			
			if($mblog_list["data"]["result"])
			{
				foreach($mblog_list["data"]["result"] as $value1)
				{
					$u_time = strtotime($value1["created_at"]);
					if($max_time < $u_time) $max_time = $u_time;
					if($min_time > $u_time || !$min_time)$min_time=$u_time;
					$create_time = date("Y-m-d H:i:s",$u_time);
					
					echo "time_compare:".strtotime($value1["created_at"])." == {$starttime}<br>\n";
					
					//如果时间跟最大时间相同，跳过
					if(strtotime($value1["created_at"]) == $starttime)
					{
						continue;
					}
					
					
					//用户信息先入库再说
					$mblog_info = $value1["user"];
					$db_user_info = new db_user_info();
					$sex = $mblog_info["gender"];
					$user_nickname = $mblog_info["name"];
					$province = $mblog_info["province"];
					$location =  $mblog_info["location"];
					$city = $mblog_info["city"];
					$desc =  $mblog_info["description"];
					$company ="";
					$school= "";
					$uid = $mblog_info["id"];
					$verified_type = $mblog_info["verified_type"];
					$birthday ="";
					
					
					
					$check_source = preg_replace("/<.*?>/","",$value1["source"]);
					
					echo $check_source."\n";
					$check_res = false;
					foreach($config["check_sina_source"] as $value)
					{
						if($check_source == $value)
						{
							$check_res = true;
							break;
						}
					}
					
						
					
					if($check_res == true)
					{
						
						$qq_user_info = $user_info->get_user_info_by_sina_uid($value1["user"]["id"]);
						
						$value1["text"] = string::un_html($value1["text"]);
						$value1["text"] = mysql_escape_string($value1["text"]);
						$date =  date("Y-m-d",strtotime($value1["created_at"]));
						$mid = $value1["mid"] ;
						$reposts_count = $value1["reposts_count"] ;
						$from=2;
						$head_url = $qq_user_info["head_url"];
						$last_insert_id = $db_mblog_meinv_content->cron_insert_mblog_meinv_content($value1["text"],$id,$topic,$value1["bmiddle_pic"],$pic_width_pubu,$pic_height_pubu,$value1["source"],$show_type,$from,$qq_user_info["uid"],$value1["user"]["screen_name"],$date,$mid,$create_time,$reposts_count,$head_url);
						
						
					
					}
				}
		
			}
				
		}
		
		if($get_type == 2)
		{
			if(!$starttime || ($min_time && $min_time < $starttime))
			{
				$db_cron_get_manage = new db_cron_get_manage();
				$get_res = $db_cron_get_manage->cron_replace_into_data($get_key,"",$min_time);
				
			}
			
		}
		if($p == 1)//当为第一页时，检查是否有多页需要抓取
		{	//最大页数为20，接口限制
			$total_page = ceil($mblog_list["data"]["count"]/50);
			$total_page = $total_page > 20 ? 20 : $total_page;
			echo "topic_page_count:{$total_page}<br>\n";
			
			if($get_type == 1)
			{
				var_dump("$max_time && $max_time > $starttime");
				if($max_time && $max_time > $starttime)
				{
					/*$sql ="update mblog_cron_topic_tag  set cron_start_time = {$max_time} where id ={$id}";
					$db_base->query($sql);*/
					$db_cron_get_manage = new db_cron_get_manage();
					$get_res = $db_cron_get_manage->cron_replace_into_data($get_key,$max_time);
				}
				
			}
			
			
			for($i = 2 ; $i <= $total_page;$i++)
			{
				$this->get_list_by_topic($topic,$id,$i,$starttime,$show_type,$black_word,$get_key,$get_type);
			}
			
		
			
			
		}
			
	}
	
	
}

$m = new mod_cron_engine_main(NOW_PROCESS);
$m->run();
